<?php
/**
 * Description: 支付完成跳转
 * User: devforma
 * Date: 15/6/9
 * Time: 13:54
 */

require PUBLIC_DIR . '/payment/includes/Alipay.class.php';

class Redirect implements iController {
    private $pcWebUrl, $hybridUrl, $mobileWebUrl;

    public function __construct() {
        $this->pcWebUrl = YG_URL . '/order/';
        $this->hybridUrl = YG_MOBILE_SECURE_URL . '/index.html#/live/ordersuccess?order_num=';
        $this->mobileWebUrl = YG_MOBILE_SECURE_URL . '/index.html#/live/mordersuccess?order_num=';
    }

    /**
     * api请求处理主函数
     * @param array $queryArgs 就是$_REQUEST变量
     * @return array api返回结果，必须含response_code
     */
    public function output($queryArgs) {
        global $cmsdb;

        $payment = new Alipay();

        //支付宝pc_web
        if (!empty($queryArgs['exterface']) && $queryArgs['exterface'] == 'create_direct_pay_by_user') {

            if ($payment->checkSign('md5', $queryArgs, true) && ($queryArgs['trade_status'] == 'TRADE_SUCCESS' ||
                    $queryArgs['trade_status'] == 'TRADE_FINISHED')) { //验证签名
                $order = $cmsdb->getRow('SELECT * FROM `relation_user_product` WHERE `order_id`=:orderid',
                    array(':orderid' => $queryArgs['out_trade_no']));
                header('Location: '.$this->pcWebUrl . $order->order_id);
                exit();
            }

        //支付宝mobile_web
        } elseif (!empty($queryArgs['request_token'])) {
            ksort($queryArgs);

            if ($payment->checkSign('md5', $queryArgs, true) && $queryArgs['result'] == 'success') {
                $order = $cmsdb->getRow('SELECT * FROM `original_orders` WHERE `order_id`=:orderid',
                    array(':orderid' => $queryArgs['out_trade_no']));

                $order->client == 'hybrid'
                    ? header('Location: '.$this->hybridUrl . $queryArgs['out_trade_no'])
                    : header('Location: '.$this->mobileWebUrl . $queryArgs['out_trade_no']);

                exit();
            }

        } else {
            echo '订单信息出错，请联系客服';
            exit();
        }

    }
}
